#pragma once
#include "Entity.h"
#include "Engine/Engine.h"
#include "Item/ItemFactory.h"
#define d_engine		Engine::Ins() 
#define d_factory		Factory::get() 
#define d_ScreenAngle	(float)(Engine::Ins()->game.getAngleToScreen())


void Entity::PC_handleGuiMessage(Vector3D Message)
{
	GUI.get().itdBeCoolIfYouUpdated();
	int index = (int)Message.x;

	switch(int(Message.z)){


	case GUI_Handler::INVENTORY:
		if(Message.x != -1)
			if(Message.y != -1)
			{
				Item_Base* tempitem = Inventory[index];
				short Type = Inventory[index]->itemType;
				switch(Type)
				{
				case ITEM_EQUIPPABLE:
					{
						short EquipType = ((Item_Equippable*)Inventory[index])->getEquipType();
						switch(EquipType)
						{
						case WEAP:	EquipItem(Weaponry,		index);	break;
						case BODY:	EquipItem(Cuirass,		index);	break;
						case FIST:	EquipItem(Gauntlets,	index); break;
						case BOOT:	EquipItem(Boots,		index);	break;
						case HEAD:	EquipItem(Helmet,		index);	break;
						}
						break;
					}
				case ITEM_CONSUMABLE:
					PC_UseConsumable(index);
					break;
				default:
					break;
				}
			}//End if(Message.y != -1)
			else
			{
				Inventory.remove(index);
				if(GUI_Handler::get().needsToRefreshInventory())
				{
					GUI.get().setInventory(Inventory);
					GUI.get().setEquipment((Item_Base**)(wornEquipment),MaxEquipables);
				}
			}
			break;
	case GUI_Handler::EQUIPMENT:
		{
			short EquipType = wornEquipment[index]->getEquipType();
			switch(EquipType)
			{
			case WEAP:	UnequipItem(Weaponry);	break;
			case BODY:	UnequipItem(Cuirass);	break;
			case FIST:	UnequipItem(Gauntlets); break;
			case BOOT:	UnequipItem(Boots);		break;
			case HEAD:	UnequipItem(Helmet);	break;
			}
			break;
			//Enters here to unequip
			//TODO- REFRESH THE GUI_SIDE PERCEPTION OF THE EQUIPMENT, VALIDATE, ETC
			/*GUI.get().setEquipment((Item_Base**)(wornEquipment),MaxEquipables);
			GUI_Handler::get().setInventory(Inventory);*/
		}
		break;
	case GUI_Handler::CRAFTABLE:

		if(Message.x != -1)
		{
			d_factory.craftObject(Inventory,index);
			GUI.get().setCrafTable(Inventory);
			GUI.get().setEquipment((Item_Base**)(wornEquipment),MaxEquipables);
		}
		break;
	}
}

void Entity::PC_handleScore(int scoretoAdd)
{
	//To use this function, call in an area where a score would be added or subtracted.
	//The parameter is the score to add
	if(entityType == P_TYPE_PLAYER)
		score += scoretoAdd;
	GUI.get().setMessage(scoretoAdd," score",GUI_Handler::NOTIFICATION);
	GUI.get().setMessage(score,"",GUI_Handler::SCORE);
}

void Entity::PC_UseConsumable(int index)
{
	bool AlreadyActive = false;
	int currStattoAffect = -1;
	int amount = -1;

	((Item_Consumable*)Inventory[index])->usesLeft--;
	if(!((Item_Consumable*)Inventory[index])->effects.isOverTime)
		for(int i = 0; i < EffectList.size(); i++)
		{
			if(compare(((Item_Consumable*)Inventory[index])->effects.effectName, EffectList[i].effectName))
			{
				AlreadyActive = true;
				((Item_Consumable*)Inventory[index])->usesLeft++;
			}
		}
		if(!AlreadyActive)
		{
			int itemtype = Inventory[index]->itemType;
			activeSounds[WAV_STIM] = true;
			for(int i = 0; i < ((Item_Consumable*)Inventory[index])->effects.curElements; i++)
			{
				currStattoAffect = ((Item_Consumable*)Inventory[index])->effects.buffList[i].statToAffect;
				amount =  ((Item_Consumable*)Inventory[index])->effects.buffList[i].amount;

				Stats.Stats[currStattoAffect] += amount;
			}
			EffectList.push(((Item_Consumable*)Inventory[index])->effects);
		}
		if(((Item_Consumable*)Inventory[index])->usesLeft == 0){
			Inventory.remove(index);
			GUI.get().setInventory(Inventory);
		}
}